/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package verins.DAO;

import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.Query;
import verins.beans.Persona;
import verins.beans.Personarnv;

/**
 *
 * @author hectdx
 */
public class PersonarnvDAO {
    
     public Personarnv findByDNI(String DNI){
        List lista=null;
        Personarnv pers=null;
        try{
            
            EntityManagerFactory emf = Persistence.createEntityManagerFactory("verinsPU");
            EntityManager em = emf.createEntityManager();
            
            Query query=em.createQuery("SELECT p FROM Personarnv p WHERE p.dni=?1") ;
            query.setParameter(1, DNI);
            lista=query.getResultList();
            em.close();    
            emf.close();
            
            if (!lista.isEmpty())
                pers=(Personarnv)lista.get(0);
            
            
        }catch(Exception ex){
            System.out.println(lista!=null?lista.size():"-"+ex.getMessage());
        }
        return pers;
    }
     
     
     
      public  List<Personarnv> findByCriterios(Personarnv personabusq){
       List<Personarnv> lista=null;
       try{
          
           String sSql="SELECT p FROM Personarnv p WHERE 1=1  ";
                    if (personabusq.getNombres().trim().length()>0)
                    sSql+= "and  upper(p.nombres)  like ?1   ";
                    if (personabusq.getApepaterno().trim().length()>0)
                    sSql+= "and  upper(p.apepaterno) like ?2   ";
                    if (personabusq.getApematerno().trim().length()>0)
                    sSql+= "and  upper(p.apematerno) like ?3   ";
                    
            EntityManagerFactory emf = Persistence.createEntityManagerFactory("verinsPU");
            EntityManager em = emf.createEntityManager();
            
            Query query=em.createQuery(sSql) ;
            if (personabusq.getNombres().trim().length()>0)
            query.setParameter(1, "%"+personabusq.getNombres().toUpperCase()+"%");
            if (personabusq.getApepaterno().trim().length()>0)
            query.setParameter(2, "%"+personabusq.getApepaterno().toUpperCase()+"%");
            if (personabusq.getApematerno().trim().length()>0)
            query.setParameter(3, "%"+personabusq.getApematerno().toUpperCase()+"%");
           
                    
            lista=query.getResultList();
            em.close();    
            emf.close();
            
            return lista;
            
        }catch(Exception ex){
            System.out.println(ex.getMessage());
        }        
        
        return lista;
    
    }
}
